package com.xmtlxx.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.IdUtil;
import com.xmtlxx.common.core.domain.PageQuery;
import com.xmtlxx.common.core.domain.entity.SysTagCheckitem;
import com.xmtlxx.common.core.page.TableDataInfo;
import com.xmtlxx.common.utils.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xmtlxx.system.domain.bo.SysTagCheckitemBo;
import com.xmtlxx.system.domain.vo.SysTagCheckitemVo;
import com.xmtlxx.system.mapper.SysTagCheckitemMapper;
import com.xmtlxx.system.service.ISysTagCheckitemService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;

/**
 * 标签与检查项关联Service业务层处理
 *
 * @author xmtlxx
 * @date 2022-04-26
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class SysTagCheckitemServiceImpl implements ISysTagCheckitemService {

    private final SysTagCheckitemMapper baseMapper;

    /**
     * 查询标签与检查项关联
     *
     * @param tid 标签与检查项关联主键
     * @return 标签与检查项关联
     */
    @Override
    public SysTagCheckitemVo queryById(String tid){
        return baseMapper.selectVoById(tid);
    }

    /**
     * 查询标签与检查项关联列表
     *
     * @param bo 标签与检查项关联
     * @return 标签与检查项关联
     */
    @Override
    public TableDataInfo<SysTagCheckitemVo> queryPageList(SysTagCheckitemBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<SysTagCheckitem> lqw = buildQueryWrapper(bo);
        Page<SysTagCheckitemVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询标签与检查项关联列表
     *
     * @param bo 标签与检查项关联
     * @return 标签与检查项关联
     */
    @Override
    public List<SysTagCheckitemVo> queryList(SysTagCheckitemBo bo) {
        LambdaQueryWrapper<SysTagCheckitem> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<SysTagCheckitem> buildQueryWrapper(SysTagCheckitemBo bo) {
        LambdaQueryWrapper<SysTagCheckitem> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getTagId()), SysTagCheckitem::getTagId, bo.getTagId());
        lqw.eq(bo.getItemId() != null, SysTagCheckitem::getItemId, bo.getItemId());
        lqw.eq(StringUtils.isNotBlank(bo.getFlag()), SysTagCheckitem::getFlag, bo.getFlag());
        lqw.orderByAsc(SysTagCheckitem::getTid);
        return lqw;
    }

    /**
     * 新增标签与检查项关联
     *
     * @param bo 标签与检查项关联
     * @return 结果
     */
    @Override
    public Boolean insertByBo(SysTagCheckitemBo bo) {
        SysTagCheckitem add = BeanUtil.toBean(bo, SysTagCheckitem.class);
        validEntityBeforeSave(add);

        //填充UUID
        add.setTid(IdUtil.simpleUUID());

        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setTid(add.getTid());
        }
        return flag;
    }

    /**
     * 修改标签与检查项关联
     *
     * @param bo 标签与检查项关联
     * @return 结果
     */
    @Override
    public Boolean updateByBo(SysTagCheckitemBo bo) {
        SysTagCheckitem update = BeanUtil.toBean(bo, SysTagCheckitem.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(SysTagCheckitem entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除标签与检查项关联
     *
     * @param ids 需要删除的标签与检查项关联主键
     * @return 结果
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

}
